<?php
  
  function decode_color($color_string = "")
  {
    $res = preg_match("/^#?(\w{1,2})(\w{1,2})(\w{1,2})$/", $color_string, $hex);
    if ($res) {
      $hex = array_slice($hex, 1);
      if (count($hex) != 3) return false;
      $rgb = array();
      for ($i = 0; $i < 3; $i++) {
        $rgb[] = round(intval((strlen($hex[$i]) == 1 ? $hex[$i].$hex[$i] : $hex[$i]), 16)/255, 1);
      }
      $ints = implode(" ", $rgb);
      return $ints;
    };
    return false;
  }

	function get_left_offset($itm, $idx) {

		$w =  $itm->coordinates->width - $itm->padding*2;
		$mw = $w / 50;
		$offs = $itm->padding;

		if($itm->barDirection == '1'){
			$offs = $w - round($mw * $idx) + $itm->padding;
		};

		return $offs;
	}

	function get_range_color($ranges, $idx) {
		$i1 = $idx * 2;
		$i2 = $i1 + 1;
//		echo 'index ', $idx, '; ';
		foreach($ranges as $r){
//			echo 'range[0] ', $r->range[0], '; ', 'range[1] ',$r->range[1], '; ';
//			echo 'color[0] ', $i1, '; ', 'color[1] ',$i2, '; ';
			
			if($r->range[0] <= $i1 && $r->range[1] >= $i2){
				return $r->color;
			}
		}
	}

	function get_skill_color($ranges, $idx) {
		foreach($ranges as $r){
			if($r->range[0] <= $idx && $r->range[1] >= $idx){
				return $r->color;
			}
		}
	}	  
    
  if (!empty($_POST) && !empty($_POST['hud_name']) && !empty($_POST['hud_data'])) {
    $config_name = $_POST['hud_name'];
    
    $json_text = stripslashes($_POST['hud_data']);
    $json = json_decode($json_text);
        
    if (!is_array($json)) {
      $json = array($json);
    }

    ob_start();
    include("templates/header.php"); 

    foreach($json as $menuItem) 
    {
      $name = ($menuItem->name == "scoreBox") ? "scoreBox_$menuItem->scoreboxStyle" : $menuItem->name;
      include("templates/$name.php");
      print_r("\n\n");
    }
	
    //include("templates/chat.php");
    print_r("\n\n");
	  
    $menu_text = ob_get_clean();
    
    ob_start();
    include("templates/config.php");
    $config_text = ob_get_clean();

    ob_start();
    include("templates/how_to_install.php");
    $readme_text = ob_get_clean();
	
    $zip = new ZipArchive; 
    $temp_name = uniqid(rand(), true);
  
    $zip_name = "../../_temp/$temp_name.zip";
  
    $res = $zip->open($zip_name, ZipArchive::CREATE);
    if ($res === TRUE) {
      try {
        $zip->addFromString("how to install.html", $readme_text);	 		
        $zip->addFromString("$config_name.menu", $menu_text);
        $zip->addFromString("$config_name.cfg", $config_text);
        $zip->addFromString("$config_name.vhud", $json_text);
        $zip->close();
        
        increment_downloads_counter();
        
        header("Pragma: public");
        header("Expires: 0");
        header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
        header("Cache-Control: private",false);
        header("Content-Type: application/octet-stream");
        header("Content-Disposition: attachment; filename=$config_name.zip");
        header("Content-Transfer-Encoding: binary");
        header("Content-Length: ".filesize($zip_name));
        ob_clean();
        flush();
        echo readfile("$zip_name");
      } catch (Exception $e) {
        echo "Oops :(";
      }
    } else {
      echo "Oops :(";
    }
  } else {
    header("Location: /");
  }
?>